#import <SpringBoard/SBApplication.h>
#import <SpringBoard/SBApplicationController.h>
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import <objc/runtime.h>
#import <UIKit/UIApplication.h>
#import "../LockInfo/Plugin.h"

#import "PITodoView.h"
#import "PIPlugin.h"


@implementation PITodoView

@synthesize dot, due, shortDue, priority, name, action, flag;

static const CGFloat dotSize = 8;
static const CGFloat markerSize = 16;
static const CGFloat detailMargin = 22;
static const CGFloat separatorSize = 4;


-(void)dealloc {
    [dot release];
    [due release];
	[shortDue release];
	[priority release];
	[name release];
	[action release];
	[flag release];

    [super dealloc];
}

+(CGFloat)nameRowHeightForFont:(UIFont*)font {
	CGFloat height = font.leading;
	if (height < markerSize)
		height = markerSize;
	return height;
}

+(CGFloat)detailRowHeightForFont:(UIFont*)font {
	CGFloat height = font.leading;
	return height;
}

+ (PITodoView *)createView:(CGRect)frame table:(LITableView *) table
{
	PITodoView* v = [[[PITodoView alloc] initWithFrame:frame] autorelease];
	v.backgroundColor = [UIColor clearColor];
	
	v.dot = [[[PIDotView alloc] initWithFrame:CGRectMake(4, 0, dotSize, dotSize)] autorelease];
	v.dot.backgroundColor = [UIColor clearColor];
	
	v.name = [table labelWithFrame:CGRectZero];
	v.name.frame = CGRectZero;
	v.name.backgroundColor = [UIColor clearColor];
	
	v.due = [table labelWithFrame:CGRectZero];
	v.due.frame = CGRectZero;
	v.due.backgroundColor = [UIColor clearColor];

	v.priority = [table labelWithFrame:CGRectZero];
	v.priority.frame = CGRectZero;
	
	v.action = [[[PIActionView alloc] initWithFrame:CGRectMake(0, 0, markerSize, markerSize)] autorelease];
	v.action.backgroundColor = [UIColor clearColor];
	
	v.flag = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, markerSize, markerSize)] autorelease];
	v.flag.backgroundColor = [UIColor clearColor];
	
	[v addSubview:v.dot];	
	[v addSubview:v.name];
	[v addSubview:v.action];
	[v addSubview:v.flag];
	[v addSubview:v.priority];
	[v addSubview:v.due];
	
	return v;
}

- (void)setViewForTable:(LITableView*)table withPlugin:(PIPlugin*)pi title:(NSString*)title due:(NSString*)dates dueShort:(NSString*)shortDates overdue:(BOOL)overdue importance:(int)importance priority:(NSString*)prty action:(int)actn flagged:(BOOL)flagged showIcons:(BOOL)showIcons
{
	self.name.style = [[table.theme.summaryStyle copy] autorelease];
	self.name.text = title;

	UIColor* importanceColor = [pi colorFromImportance:importance];
	if (importanceColor != nil) {
		self.dot.color = importanceColor;
		self.dot.hidden = false;
		[self.dot setNeedsDisplay];
	}
	else
		self.dot.hidden = true;

	if ((dates != nil) && (prty != nil) && ([prty length] > 0)) {
		self.priority.text = prty;
		self.priority.style = [[table.theme.detailStyle copy] autorelease];
		self.priority.style.textColor = self.name.style.textColor;
		self.priority.layer.cornerRadius = 6.0;
		self.priority.layer.masksToBounds= YES;
		self.priority.backgroundColor = [importanceColor colorWithAlphaComponent:0.85];
		self.priority.hidden = false;
	}
	else
		self.priority.hidden = true;
		
	if (flagged && showIcons) {
		NSString* imagePath = [NSString stringWithFormat:@"%@/%@.png", [pi appPath], @"star_yellow_small"];
		DebugLog(@"LI:PITodo: Loading flag image %@", imagePath);

		self.flag.image = [UIImage imageWithContentsOfFile:imagePath];
		self.flag.hidden = false;
	}
	else
		self.flag.hidden = true;
	
	if (dates != nil) {
		if (overdue) {
			self.due.style = [[table.theme.detailStyle copy] autorelease];
			self.due.style.textColor = [pi overdueColor];
		}
		else
			self.due.style = [[table.theme.detailStyle copy] autorelease];

		self.due.text = dates;
		self.due.hidden = false;
		self.shortDue = shortDates;
	}
	else
		self.due.hidden = true;
	
	NSString* actionIcon = [pi iconPathFromAction:actn];
	if (showIcons && (actionIcon != nil)) {
		self.action.color = [pi colorFromAction:actn];
		self.action.icon = actionIcon;
		[self.action setNeedsDisplay];
		self.action.hidden = false;
	}
	else
		self.action.hidden = true;
	
	[self setNeedsLayout];
}

- (void)layoutSubviews {
	CGFloat nameHeight = [PITodoView nameRowHeightForFont:self.name.style.font];
	CGFloat detailHeight = [PITodoView detailRowHeightForFont:self.due.style.font];
	
	if (!self.dot.hidden) {
		// Place the dot half way up the title line
		CGRect rect = self.dot.frame;
		CGFloat pos = (nameHeight - rect.size.height) / 2;
		if (pos < 0)
			pos = 0;
		self.dot.frame = CGRectMake(rect.origin.x, pos, rect.size.width, rect.size.height);
	}

	// Size of the screen
	int rightMargin = self.frame.size.width - separatorSize - 1;

	if (!self.due.hidden) {
		int leftMargin = detailMargin;
		
		// If we have a priority then allow that as much room as it needs
		if (!self.priority.hidden) {
			CGFloat dueWidth = [self.due.text sizeWithFont:self.due.style.font].width;
			CGFloat padding = self.priority.layer.cornerRadius * 2;
			CGFloat priorityWidth = [self.priority.text sizeWithFont:self.priority.style.font].width + padding;
			
			// Is the line too long? If we we'll reduce the project size
			if ((leftMargin + priorityWidth + separatorSize + dueWidth) > rightMargin) {
				CGFloat shortWidth = [self.shortDue sizeWithFont:self.due.style.font].width;
				if (shortWidth < dueWidth) {
					self.due.text = self.shortDue;
					dueWidth = shortWidth;
				}
				
				if ((leftMargin + priorityWidth + separatorSize + dueWidth) > rightMargin)
					priorityWidth = rightMargin - leftMargin - separatorSize - dueWidth;
			}
			
			CGRect rect = CGRectMake(leftMargin, nameHeight, priorityWidth, detailHeight);
			leftMargin += rect.size.width + separatorSize;
			self.priority.frame = rect;
			[self.priority setTextAlignment:UITextAlignmentCenter];
		}

		CGRect rect = CGRectMake(leftMargin, nameHeight, self.frame.size.width - leftMargin, detailHeight);
		self.due.frame = rect;
	}

	if (!self.action.hidden) {
		// Put action half way up the name line unless it is too small
		CGRect rect = self.action.frame;
		CGFloat pos = (nameHeight - rect.size.height) / 2;
		if (pos < 0)
			pos = 0;
		rect = CGRectMake(rightMargin - rect.size.width, pos, rect.size.width, rect.size.height);
		rightMargin = rightMargin - rect.size.width - separatorSize;
		self.action.frame = rect;
	}

	if (!self.flag.hidden) {
		// Put flag half way up the name line unless it is too small
		CGRect rect = self.flag.frame;
		CGFloat pos = (nameHeight - rect.size.height) / 2;
		if (pos < 0)
			pos = 0;
		rect = CGRectMake(rightMargin - rect.size.width, pos, rect.size.width, rect.size.height);
		rightMargin = rightMargin - rect.size.width - separatorSize;
		self.flag.frame = rect;
	}
	
	CGRect rect = CGRectMake(detailMargin, 0, rightMargin - detailMargin, nameHeight);
	self.name.frame = rect;
}

@end
